<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
          integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">

    <!-- 可选的 Bootstrap 主题文件（一般不用引入） -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap-theme.min.css"
          integrity="sha384-6pzBo3FDv/PJ8r2KRkGHifhEocL+1X2rVCTTkUfGk7/0pbek5mMa1upzvWbrUbOZ" crossorigin="anonymous">

    <style>
        .breadcrumb {
            padding: 15px 15px;
            background-color: transparent;
            margin-bottom: 0px;
            display: inline-block;
        }

        .pull-right {
            padding-top: 7px;
        }

        .inline-block {
            display: inline-block;
        }
    </style>
</head>
<body>
<nav class="navbar navbar-default">
    <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">书本商城</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ol class="breadcrumb">
                <li><a href="#">首页</a></li>
                <li class="active">购物车</li>
            </ol>
            <div class="btn-group pull-right" role="group" aria-label="...">
                <button type="button" class="btn btn-danger">修改密码</button>
                <button type="button" class="btn btn-danger">注销</button>
            </div>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
</nav>
<div class="container">
    <div class="panel panel-default">
        <div class="panel-heading">
            <form class="form-inline inline-block">
                <div class="form-group">
                    <input type="text" class="form-control input-sm" placeholder="书本">
                </div>
                <div class="form-group">
                    <input type="number" class="form-control input-sm" placeholder="最低价">
                </div>
                <div class="form-group">
                    <input type="number" class="form-control input-sm" placeholder="最高价">
                </div>
                <button type="button" class="btn btn-default btn-sm">
                    <span class="glyphicon glyphicon-search" aria-hidden="true"></span>
                </button>
            </form>
            <div class="inline-block" style="float: right;">
                <button type="button" class="btn btn-success">添加</button>
                <button type="button" class="btn btn-success">批量删除</button>
            </div>
        </div>
        <div class="panel-body">
            <div id="table_div">

            </div>
            <!--            <table class="table table-hover table-bordered">-->
            <!--                <caption>Optional table caption.</caption>-->
            <!--                <thead>-->
            <!--                <tr>-->
            <!--                    <th>#</th>-->
            <!--                    <th>First Name</th>-->
            <!--                    <th>Last Name</th>-->
            <!--                    <th>Username</th>-->
            <!--                </tr>-->
            <!--                </thead>-->
            <!--                <tbody>-->
            <!--                <tr>-->
            <!--                    <th scope="row">1.txt</th>-->
            <!--                    <td>Mark</td>-->
            <!--                    <td>Otto</td>-->
            <!--                    <td>@mdo</td>-->
            <!--                </tr>-->
            <!--                <tr>-->
            <!--                    <th scope="row">2</th>-->
            <!--                    <td>Jacob</td>-->
            <!--                    <td>Thornton</td>-->
            <!--                    <td>@fat</td>-->
            <!--                </tr>-->
            <!--                <tr>-->
            <!--                    <th scope="row">3</th>-->
            <!--                    <td>Larry</td>-->
            <!--                    <td>the Bird</td>-->
            <!--                    <td>@twitter</td>-->
            <!--                </tr>-->
            <!--                </tbody>-->
            <!--            </table>-->
            <nav class="text-center" aria-label="Page navigation">
                <ul class="pagination">
                    <li>
                        <a href="#" aria-label="Previous">
                            <span aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    <li><a href="#">1</a></li>


                    <li><a href="#">2</a></li>
                    <li><a href="#">3</a></li>
                    <li><a href="#">4</a></li>
                    <li><a href="#">5</a></li>
                    <li>
                        <a href="#" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                </ul>
            </nav>
        </div>
        <div class="panel-footer">Panel footer</div>
    </div>
</div>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<!--bootstrap的JS是需要Jquery的支持-->
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"
        integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd"
        crossorigin="anonymous"></script>
<script>
    //获取tbody标签
    // let tbodyColl = document.getElementsByTagName("tbody");
    // let tbody = tbodyColl[0];
    // let tbody = document.querySelector("tbody");
    // while (tbody.hasChildNodes()) {
    //     tbody.firstChild.remove();
    // }
    // while (tbody.childNodes.length) {
    //     tbody.childNodes[tbody.childNodes.length - 1.txt].remove();
    // }
    // while (tbody.children.length) {
    //     tbody.children[tbody.children.length - 1.txt].remove();
    // }
    // Array.from(tbody.children).forEach(function (value) {
    //     console.log(value);
    //     tbody.removeChild(value);
    // })
    // let tdAndTh = tbody.querySelectorAll("td,th");
    // console.log(tdAndTh);
    // let tds = tbody.getElementsByTagName("td");
    // let ths = tbody.getElementsByTagName("th");
    // let eles = Array.from(ths).concat(Array.from(tds));
    // console.log(eles);
    // eles.forEach(function (value) {
    //     value.innerText = "";
    // });

    let table_div = document.querySelector("#table_div");
    let table = document.createElement("table");
    table_div.appendChild(table);
    //会覆盖原来的样式
    table.className = "table table-hover table-bordered";
    // table.classList

    let thead = document.createElement("thead");
    table.appendChild(thead);
    let tbody = document.createElement("tbody");
    table.appendChild(tbody);

    let tr = document.createElement("tr");
    thead.appendChild(tr);
    let titles = ["<input type='checkbox'>", "序号", "书名", "日期", "单价", "数量", "操作"];
    titles.forEach(function (value, index) {
        let th = document.createElement("th");
        tr.appendChild(th);
        th.innerHTML = value;
        // th.className = "text-center";
        th.classList.add("text-center");
        // th.style.css属性  = css属性值，一般不用
    });

    let books = [
        {id: 1, name: 'Java从入门到放弃', price: 80, tdate: '2020-07-01', count: 1},
        {id: 2, name: 'Vue从入门到放弃', price: 90, tdate: '2020-07-02', count: 1},
        {id: 3, name: 'Mysql从入门到放弃', price: 100.01, tdate: '2020-07-03', count: 1},
        {id: 4, name: '啥都从入门到放弃', price: 70.00, tdate: '2020-07-04', count: 1},
    ];

    books.forEach(function (value, index) {
        tr = document.createElement("tr");
        tbody.appendChild(tr);
        let tds = `
            <td><input type="checkbox"></td>
            <td>${value.id}</td>
            <td>${value.name}</td>
            <td>${value.tdate}</td>
            <td>${value.price}</td>
            <td>${value.count}</td>
            <td>
                <button type="button">编辑</button>
                <button type="button">删除</button>
            </td>
        `;
        tr.innerHTML = tds;
    });

    let tds = document.querySelectorAll("tbody tr td");
    console.log(tds);
    Array.from(tds).forEach(function (value) {
        //不会覆盖原来的样式
        value.classList.add("text-center");
    });

</script>
</body>
</html>